﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using TEL.ClientClasses;
using TEL.ClientClasses.SipInterfaces;

namespace TEL.TelPhoneF.Controls
{
    public partial class OptionPannel : CustomPanel, IPhoneOption
    {
        private object mLock = new object();
        private GraphicsPath m_MainBtnPath;
        public OptionPannel()
        {
            InitializeComponent();

            foreach (Control c in this.Controls)
            {
                c.KeyDown += KeyDownEvent;
            }

            m_MainBtnPath = new GraphicsPath();
            m_MainBtnPath.AddEllipse(new Rectangle(1, 1, 46, 46));
            btnMain.Region = new Region(m_MainBtnPath);

            Paths paths = new Paths();
            btnSet.Region = new Region(paths.FairingLeftPath(new Point(1, 1), btnSet.Size - new Size(2, 2)));
        }

        #region Implementation of IPhoneOption

        public bool AutoAccept
        {
            get { return btnAC.State; }
            set { btnAC.State = value; }
        }

        public bool dnd
        {
            get { return btnDND.State; }
            private set
            {
                btnDND.State = value;
                if (DNDChanged != null) DNDChanged(this, new BoolEventArgs(value));
            }
        }

        public void SetDND(bool value)
        {
            dnd = value;
        }

        public event EventHandler<BoolEventArgs> DNDChanged;

        public bool Saving
        {
            get { return btnSave.State; }
            set
            {
                btnSave.State = value;
                if (SavingChanged != null) SavingChanged(this, new BoolEventArgs(value));
            }
        }

        public event EventHandler<BoolEventArgs> SavingChanged;

        public bool SaveEnable
        { get { return btnSave.Enabled; } }

        public void SetSaveEnable(bool value)
        {
            lock (mLock)
            {
                try
                {


                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          if (!value) Saving = value;
                                                          btnSave.Enabled = value;
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public bool Conf
        {
            get { return btnConf.State; }
            set
            {
                btnConf.State = value;
                if (ConfChanged != null)
                    ConfChanged(this, new BoolEventArgs(value));
            }
        }

        public event EventHandler<BoolEventArgs> ConfChanged;

        public bool Transfer
        {
            get { return btnTransfer.State; }
            set
            {
                btnTransfer.State = value;
                if (TransferChanged != null) TransferChanged(this, new BoolEventArgs(value));
            }
        }

        public bool Xfer
        {
            get { return btnXfer.State; }
            set
            {
                btnXfer.State = value;
                if (XferChanged != null) XferChanged(this, new BoolEventArgs(value));
            }
        }

        public event EventHandler<BoolEventArgs> XferChanged;
        public event EventHandler XferClick;

        public event EventHandler<BoolEventArgs> TransferChanged;
        public event EventHandler TransferClick;
        public event EventHandler MainClickEvent;
        public event EventHandler ClearEvent;

        public bool AutoRedial 
        { get { return btnAR.State; } 
            set { btnAR.State = value; } }

        public void AutoAcceptMainClick()
        {
            try
            {
                this.Invoke(new MethodInvoker(delegate()
                                                  {
                                                      btnMain.PerformClick();
                                                  }), null);
            }
            catch (Exception)
            {
            }
        }

        public event EventHandler RedialEvent;

        #endregion

        private void btnAC_Click(object sender, EventArgs e)
        {
            AutoAccept = !AutoAccept;
        }

        private void btnDND_Click(object sender, EventArgs e)
        {
            SetDND(!dnd);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Saving = !Saving;
        }

        private void btnConf_Click(object sender, EventArgs e)
        {
            Conf = !Conf;
        }

        public bool BtnPanel_KeyDown(KeyEventArgs e) 
        {
            bool res = false;
            if (e.Alt || e.Control || e.Shift) return false;
            switch (e.KeyCode)
            {
                case Keys.A:
                    //btnAC.PerformClick();
                    btnAC_Click(this, EventArgs.Empty);
                    res = true;
                    break;
                case Keys.D:
                  //  btnDND.PerformClick();
                    btnDND_Click(this, EventArgs.Empty);
                    res = true;
                    break;
                case Keys.S:
                    //btnSave.PerformClick();
                    btnSave_Click(this, EventArgs.Empty);
                    res = true;
                    break;
                case Keys.C:
                   // btnConf.PerformClick();
                    btnConf_Click(this, EventArgs.Empty);
                    res = true;
                    break;
                case Keys.P:
                    btnSet.PerformClick();
                    res = true;
                    break;
                case Keys.Enter:
                    btnMain.PerformClick();
                    btnMain_Click(this, EventArgs.Empty);
                    res = true;
                    break;
                //case Keys.Escape:
                //    BtnDoun.PerformClick(); res = true; 
            }
            return res;
        }

        private bool m_BtnMainEnter = false;
        private bool m_BtnMainDown = false;

        private void btnMain_Paint(object sender, PaintEventArgs e)
        {
            if (m_BtnMainDown)
            {
                Brush b = new SolidBrush(Color.FromArgb(64, Form1.BlueSkinColor));
                Rectangle rectangle = new Rectangle(new Point(0, 0), this.Size);
                e.Graphics.FillRectangle(b, rectangle);
                return;
            }

            if (m_BtnMainEnter)
            {
                Brush b = new SolidBrush(Color.FromArgb(36, Form1.BlueSkinColor));
                Rectangle rectangle = new Rectangle(new Point(0, 0), this.Size);
                e.Graphics.FillRectangle(b, rectangle);
            }
        }

        private void btnMain_Click(object sender, EventArgs e)
        {
            if (MainClickEvent != null) MainClickEvent.BeginInvoke(this, EventArgs.Empty,null,null);
        }

        private void OptionPannel_Paint(object sender, PaintEventArgs e)
        {
        }

        private void btnXfer_Click(object sender, EventArgs e)
        {
            if (XferClick != null) XferClick(this, EventArgs.Empty);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (ClearEvent != null) ClearEvent(this, EventArgs.Empty);
        }

        private void btnRedial_Click(object sender, EventArgs e)
        {
            if (RedialEvent != null) RedialEvent(this, EventArgs.Empty);
        }

        private void btnMain_MouseEnter(object sender, EventArgs e)
        {
            m_BtnMainEnter = true;
        }

        private void btnMain_MouseLeave(object sender, EventArgs e)
        {
            m_BtnMainEnter = false;
        }

        private void btnMain_MouseDown(object sender, MouseEventArgs e)
        {
            m_BtnMainDown = true;
        }

        private void btnMain_MouseUp(object sender, MouseEventArgs e)
        {
            m_BtnMainDown = false;
            btnMain.Invalidate();
        }

        private void btnTransfer_Click(object sender, EventArgs e)
        {
            if (TransferClick != null) TransferClick(this, EventArgs.Empty);
        }

        private TransferType m_transferOption;
        public TransferType TransferOption
        {
            get { return m_transferOption; }
            set
            {
                lock (mLock)
                {
                    try
                    {
                        this.Invoke(new MethodInvoker(delegate()
                                                          {
                                                              m_transferOption = value;
                                                              switch (value)
                                                              {
                                                                  case TransferType.Transfer:
                                                                      btnTransfer.Visible = true;
                                                                      btnXfer.Visible = false;
                                                                      break;
                                                                  case TransferType.Xfer:
                                                                      btnTransfer.Visible = false;
                                                                      btnXfer.Visible = true;
                                                                      break;
                                                              }
                                                          }), null);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        private void btnAR_Click(object sender, EventArgs e)
        {
            AutoRedial = !AutoRedial;
        }
    }
}
